﻿using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework;
using NHibernate.Criterion;
using Online_Course.Core;
using Online_Course.Domain;
using Online_Course.Service;
using Online_Course.Web.Apps;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
    
namespace Online_Course.Web
{
    public partial class InitDataBase : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnCreateDB_Click(object sender, EventArgs e)
        {
            string ret = "<br />";

            if (!ActiveRecordStarter.IsInitialized)
            {
                //如果ActiveRecordStarter框架没有初始化
                IConfigurationSource source = System.Configuration.ConfigurationManager.GetSection("activerecord") as IConfigurationSource;
                ActiveRecordStarter.Initialize(typeof(SysUser).Assembly, source);
            }
            ret += "正在创建数据库...<br />";
            ActiveRecordStarter.CreateSchema();
            ret += "创建数据库完成<br />";

            ret += "正在初始化数据...<br/>";
            InitSystemFunction();
            ret += "初始化菜单完毕！<br/>";

            InitMajor();
            ret += "初始化专业完毕！<br/>";

            InitTeacher();
            ret += "初始化教师完毕！<br/>";

            InitCourse();
            ret += "初始化课程完毕！<br/>";

            InitSysRole();
            ret += "初始化角色完毕！<br/>";

            InitSysUser();
            ret += "初始化用户完毕！<br/>";

            InitStudent();
            ret += "初始化学生完毕！<br/>";

            //InitStudent_Course();
            //ret += "初始化选课完毕！<br/>";

            ret += "初始化数据完成！<br/>";
            Response.Write(ret);
        }

        /// <summary>
        /// 初始化菜单
        /// </summary>
        private void InitSystemFunction()
        {
            #region 系统管理
            //id=1
            Container.Instance.Resolve<ISystemFunctionService>().Add(new SystemFunction
                {
                    Name="系统管理",
                    IsShow=true,
                    SortCode=1,
                });
            //id=2
            Container.Instance.Resolve<ISystemFunctionService>().Add(new SystemFunction
            {
                Name = "角色管理",
                ClazzName = "Online_Course.Web.Controllers.SysRoleController",
                ConName = "SysRole",
                IsShow = true,
                ActionName = "Index",
                SortCode = 2,
                ParentSysMenu = Container.Instance.Resolve<ISystemFunctionService>().GetEntity(1)
            });
            //id=3
            Container.Instance.Resolve<ISystemFunctionService>().Add(new SystemFunction()
            {
                Name = "用户管理",
                ClazzName = "Online_Course.Web.Controllers.SysUserController",
                ConName = "SysUser",
                IsShow = true,
                ActionName = "Index",
                SortCode = 3,
                ParentSysMenu = Container.Instance.Resolve<ISystemFunctionService>().GetEntity(1)
            });
            #endregion

            #region 基础数据管理
            //id=4
            Container.Instance.Resolve<ISystemFunctionService>().Add(new SystemFunction()
            {
                Name = "基础数据管理",
                SortCode = 4,
                IsShow=true
            });
            //id=5
            Container.Instance.Resolve<ISystemFunctionService>().Add(new SystemFunction()
            {
                Name = "课程管理",
                ClazzName = "Online_Course.Web.Controllers.CourseController",
                ConName = "Course",
                ActionName = "Index",
                IsShow=true,
                SortCode = 5,

                ParentSysMenu = Container.Instance.Resolve<ISystemFunctionService>().GetEntity(4)
            });

            //id=6
            Container.Instance.Resolve<ISystemFunctionService>().Add(new SystemFunction()
            {
                Name = "教师管理",
                ClazzName = "Online_Course.Web.Controllers.TeacherController",
                ConName = "Teacher",
                IsShow = true,
                ActionName = "Index",
                SortCode = 6,

                ParentSysMenu = Container.Instance.Resolve<ISystemFunctionService>().GetEntity(4)
            });

            //id=7
            Container.Instance.Resolve<ISystemFunctionService>().Add(new SystemFunction()
            {
                Name = "学生管理",
                ClazzName = "Online_Course.Web.Controllers.StudentController",
                ConName = "Student",
                IsShow = true,
                ActionName = "Index",
                SortCode = 7,

                ParentSysMenu = Container.Instance.Resolve<ISystemFunctionService>().GetEntity(4)
            });

            #endregion

            #region 业务数据管理
            //id=8
            Container.Instance.Resolve<ISystemFunctionService>().Add(new SystemFunction()
            {
                Name = "业务管理",
                SortCode = 8,
                IsShow=true
            });
            //id=9
            Container.Instance.Resolve<ISystemFunctionService>().Add(new SystemFunction()
            {
                Name = "查看选课",
                ClazzName = "Online_Course.Web.Controllers.Student_CourseController",
                ConName = "Student_Course",
                IsShow = true,
                ActionName = "Index",
                SortCode = 9,
                ParentSysMenu = Container.Instance.Resolve<ISystemFunctionService>().GetEntity(8)
            });
            #endregion

            #region 教师任课表
            //id=10
            Container.Instance.Resolve<ISystemFunctionService>().Add(new SystemFunction()
            {
                Name = "任课表",
                ClazzName = "Online_Course.Web.Controllers.CourseController",
                ConName = "Course",
                IsShow = true,
                ActionName = "TeacherIndex",
                SortCode = 10,
                ParentSysMenu = Container.Instance.Resolve<ISystemFunctionService>().GetEntity(4)
            });
            #endregion

            #region 学生选课表
            //id=11
            Container.Instance.Resolve<ISystemFunctionService>().Add(new SystemFunction()
            {
                Name = "选课",
                ClazzName = "Online_Course.Web.Controllers.CourseController",
                ConName = "Course",
                IsShow = true,
                ActionName = "StudentIndex",
                SortCode = 11,
                ParentSysMenu = Container.Instance.Resolve<ISystemFunctionService>().GetEntity(4)
            });
            #endregion

            #region 教师审核表
            //id=12
            Container.Instance.Resolve<ISystemFunctionService>().Add(new SystemFunction()
            {
                Name = "审核选课",
                ClazzName = "Online_Course.Web.Controllers.Student_CourseController",
                ConName = "Student_Course",
                IsShow = true,
                ActionName = "Audit",
                SortCode = 12,
                ParentSysMenu = Container.Instance.Resolve<ISystemFunctionService>().GetEntity(8)
            });
            #endregion

            #region 查看选课结果
            //id=13
            Container.Instance.Resolve<ISystemFunctionService>().Add(new SystemFunction()
            {
                Name = "查看选课结果",
                ClazzName = "Online_Course.Web.Controllers.Student_CourseController",
                ConName = "Student_Course",
                IsShow = true,
                ActionName = "StudentIndex",
                SortCode = 13,
                ParentSysMenu = Container.Instance.Resolve<ISystemFunctionService>().GetEntity(8)
            });
            #endregion

            #region 所有选课列表
            //id=14
            Container.Instance.Resolve<ISystemFunctionService>().Add(new SystemFunction()
            {
                Name = "所有选课结果",
                ClazzName = "Online_Course.Web.Controllers.Student_CourseController",
                ConName = "Student_Course",
                IsShow = true,
                ActionName = "AllIndex",
                SortCode = 14,
                ParentSysMenu = Container.Instance.Resolve<ISystemFunctionService>().GetEntity(8)
            });
            #endregion

            #region 统计分析表
            //id=15
            Container.Instance.Resolve<ISystemFunctionService>().Add(new SystemFunction()
            {
                Name = "统计分析",
                ClazzName = "Online_Course.Web.Controllers.Student_CourseController",
                ConName = "Student_Course",
                IsShow = true,
                ActionName = "Analysis",
                SortCode = 15,
                ParentSysMenu = Container.Instance.Resolve<ISystemFunctionService>().GetEntity(8)
            });
            #endregion
        }

        /// <summary>
        /// 初始化角色
        /// </summary>
        private void InitSysRole()
        {
            Container.Instance.Resolve<ISysRoleService>().Add(new SysRole()
            {
                Name = "系统管理员",
                IsActive = 1,
                SystemFunctionList = Container.Instance.Resolve<ISystemFunctionService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 1, 2, 3, 4, 5, 6, 7, 8,14,15}) })
            });

            Container.Instance.Resolve<ISysRoleService>().Add(new SysRole()
            {
                Name = "学生",
                IsActive = 1,
                SystemFunctionList = Container.Instance.Resolve<ISystemFunctionService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 4,11,8,13 }) })
            });

            Container.Instance.Resolve<ISysRoleService>().Add(new SysRole()
            {
                Name = "教师",
                IsActive = 1,
                SystemFunctionList = Container.Instance.Resolve<ISystemFunctionService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 4,10,8,9,12 }) })
            });
        }

        /// <summary>
        /// 初始化用户
        /// </summary>
        private void InitSysUser()
        {
            //id=1
            Container.Instance.Resolve<ISysUserService>().Add(new SysUser()
            {
                Name = "李永富",
                Password = AppHelper.EncodeMd5("123456"),
                Account = "lyf",
                IsActive = 1,
                SysRoleList = Container.Instance.Resolve<ISysRoleService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 1, 2 ,3}) })//拥有管理员学生和教师角色
            });
            //id=2
            Container.Instance.Resolve<ISysUserService>().Add(new SysUser()
            {
                Name = "李霜",
                Password = AppHelper.EncodeMd5("123456"),
                Account = "ls",
                Phone = "1888888888",
                IsActive = 1,
                SysRoleList = Container.Instance.Resolve<ISysRoleService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 3 }) })//拥有教师角色
            });
            //id=3
            Container.Instance.Resolve<ISysUserService>().Add(new SysUser()
            {
                Name = "冷亚洪",
                Password = AppHelper.EncodeMd5("123456"),
                Account = "lyh",
                Phone = "1888888888",
                IsActive = 1,
                SysRoleList = Container.Instance.Resolve<ISysRoleService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 1,3 }) })//拥有教师和管理员角色
            });
            //id=4
            Container.Instance.Resolve<ISysUserService>().Add(new SysUser()
            {
                Name = "张浩然",
                Password = AppHelper.EncodeMd5("123456"),
                Account = "zhr",
                Phone = "1888888888",
                IsActive = 1,
                SysRoleList = Container.Instance.Resolve<ISysRoleService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] {  3 }) })//拥有教师和管理员角色
            });
            //id=5
            Container.Instance.Resolve<ISysUserService>().Add(new SysUser()
            {
                Name = "高峰",
                Password = AppHelper.EncodeMd5("123456"),
                Account = "gf",
                Phone = "1888888888",
                IsActive = 1,
                SysRoleList = Container.Instance.Resolve<ISysRoleService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 3 }) })//拥有教师和管理员角色
            });

            //id=6
            Container.Instance.Resolve<ISysUserService>().Add(new SysUser()
            {
                Name = "秦琴",
                Password = AppHelper.EncodeMd5("123456"),
                Account = "qq",
                Phone = "1888888888",
                IsActive = 1,
                SysRoleList = Container.Instance.Resolve<ISysRoleService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 3 }) })//拥有教师和管理员角色
            });
            //id=7
            Container.Instance.Resolve<ISysUserService>().Add(new SysUser()
            {
                Name = "张景东",
                Password = AppHelper.EncodeMd5("123456"),
                Account = "zjd",
                Phone = "1888888888",
                IsActive = 1,
                SysRoleList = Container.Instance.Resolve<ISysRoleService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 3 }) })//拥有教师和管理员角色
            });
            //id=8
            Container.Instance.Resolve<ISysUserService>().Add(new SysUser()
            {
                Name = "系统管理员",
                Password = AppHelper.EncodeMd5("123456"),
                Account = "Admin",
                IsActive = 1,
                SysRoleList = Container.Instance.Resolve<ISysRoleService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] {1}) })//管理员
            });
        }

        /// <summary>
        /// 初始化专业
        /// </summary>
        private void InitMajor()
        {
            //id=1
            Core.Container.Instance.Resolve<IMajorService>().Add(new Major()
            {
                Name = "软件工程",
                Code="001"
            });

            //id=2
            Core.Container.Instance.Resolve<IMajorService>().Add(new Major()
            {
                Name = "电子信息工程",
                Code="002"
            });
            //id=3
            Core.Container.Instance.Resolve<IMajorService>().Add(new Major()
            {
                Name = "网络工程",
                Code="003"
            });
            //id=4
            Core.Container.Instance.Resolve<IMajorService>().Add(new Major()
            {
                Name = "物联网工程",
                Code="004"
            });
            //id=5
            Core.Container.Instance.Resolve<IMajorService>().Add(new Major()
            {
                Name = "工程造价",
                Code="005"
            });
            //id=6
            Core.Container.Instance.Resolve<IMajorService>().Add(new Major()
            {
                Name = "数字媒体技术",
                Code="006"
            });
        }

        /// <summary>
        /// 初始化学生
        /// </summary>
        /// 
        private void InitStudent()
        {
            //id=1
            Container.Instance.Resolve<IStudentService>().Add(new Student()
            {
                Name = "李永富",
                Code="169000531",
                Phone = "18290403841",
                IsActive = 1,
            });

        }

        /// <summary>
        /// 初始化教师
        /// </summary>
        private void InitTeacher()
        {

            //id=1
            Container.Instance.Resolve<ITeacherService>().Add(new Teacher()
            {
                Name = "李永富",
                TeacherCode = "169000531",
                CourseList = Container.Instance.Resolve<ICourseService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 2, 6 }) }),
                Phone = "18290403841",
                IsActive = 1,
            });
            //id=1
            Container.Instance.Resolve<ITeacherService>().Add(new Teacher()
            {
                Name = "李霜",
                TeacherCode = "169001",
                CourseList = Container.Instance.Resolve<ICourseService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 2, 6 }) }),
                Phone = "1888888888",
                IsActive = 1,
            });
            //id=3
            Container.Instance.Resolve<ITeacherService>().Add(new Teacher()
            {
                Name = "冷亚洪",
                TeacherCode = "169011",
                Phone = "1888888888",
                CourseList = Container.Instance.Resolve<ICourseService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 3 }) }),
                IsActive = 1,
            });
                
            //id=2
            Container.Instance.Resolve<ITeacherService>().Add(new Teacher()
            {
                Name = "张浩然",
                TeacherCode = "169012",
                CourseList = Container.Instance.Resolve<ICourseService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 8, 7, 5 }) }),
                Phone = "1888888888",
                IsActive = 1,
            });


            //id=4
            Container.Instance.Resolve<ITeacherService>().Add(new Teacher()
            {
                Name = "高峰",
                TeacherCode = "169013",
                Phone = "1888888888",
                CourseList = Container.Instance.Resolve<ICourseService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 4 }) }),
                IsActive = 1,
            });
            //id=5
            Container.Instance.Resolve<ITeacherService>().Add(new Teacher()
            {
                Name = "秦琴",
                TeacherCode = "179011",
                Phone = "1888888888",
                CourseList = Container.Instance.Resolve<ICourseService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 1, 11 }) }),
                IsActive = 1,
            });
            //id=6
            Container.Instance.Resolve<ITeacherService>().Add(new Teacher()
            {
                Name = "张景东",
                TeacherCode = "179012",
                Phone = "1888888888",
                CourseList = Container.Instance.Resolve<ICourseService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 5, 7, 8 }) }),
                IsActive = 1,
            });
        }

        /// <summary>
        /// 初始化课程
        /// </summary>
        private void InitCourse()
        {
            //id=1
            Container.Instance.Resolve<ICourseService>().Add(new Course()
            {
                Name = "沟通心理学",
                CourseCode = "001",
                Maxpeople=40,
                ChoosePeople=0,
                TeacherList = Container.Instance.Resolve<ITeacherService>().Query(new List<ICriterion>(){Expression.In("ID",new int[]{5})}),
                Remark = "人与人的了解从沟通开始"

            });
            //id=2
            Container.Instance.Resolve<ICourseService>().Add(new Course()
            {
                Name = "唐诗宋词人文鉴赏",
                CourseCode = "002",
                Maxpeople = 40,
                ChoosePeople = 0,
                TeacherList = Container.Instance.Resolve<ITeacherService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 6 }) }),
                Remark = "了解古代文人的生活方式与人生观点"

            });
            //id=3
            Container.Instance.Resolve<ICourseService>().Add(new Course()
            {
                Name = "标准日本语",
                CourseCode = "003",
                Maxpeople = 40,
                ChoosePeople = 21,
                TeacherList = Container.Instance.Resolve<ITeacherService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 3 }) }),
                Remark = "体验不一样的日本文化，daisiki"
            });
            //id=4
            Container.Instance.Resolve<ICourseService>().Add(new Course()
            {
                Name = "电影鉴赏",
                CourseCode = "004",
                Maxpeople = 40,
                ChoosePeople = 0,
                TeacherList = Container.Instance.Resolve<ITeacherService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 4 }) }),
                Remark = "深度解读经典电影"
            });
            //id=5
            Container.Instance.Resolve<ICourseService>().Add(new Course()
            {
                Name = "电竞知识基础",
                CourseCode = "005",
                Maxpeople = 40,
                ChoosePeople = 0,
                TeacherList = Container.Instance.Resolve<ITeacherService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 6 }) }),
                Remark = "走进游戏，体验游戏中的人生"
            });
            //id=6
            Container.Instance.Resolve<ICourseService>().Add(new Course()
            {
                Name = "恋爱管家",
                CourseCode = "006",
                Maxpeople = 50,
                ChoosePeople = 48,
                TeacherList = Container.Instance.Resolve<ITeacherService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 1 }) }),
                Remark = "手把手教你如何吸引异性"
            });
            //id=7
            Container.Instance.Resolve<ICourseService>().Add(new Course()
            {
                Name = "Adobe全家桶",
                CourseCode = "007",
                Maxpeople = 50,
                ChoosePeople = 37,
                TeacherList = Container.Instance.Resolve<ITeacherService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 6 }) }),
                Remark = "大师修炼必备"
            });
            //id=8
            Container.Instance.Resolve<ICourseService>().Add(new Course()
            {
                Name = "Office智能办公",
                CourseCode = "008",
                Maxpeople = 40,
                ChoosePeople = 0,
                TeacherList = Container.Instance.Resolve<ITeacherService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 6 }) }),
                Remark = "教你用更轻松的方法完成繁琐的工作"
            });
            //id=9
            Container.Instance.Resolve<ICourseService>().Add(new Course()
            {
                Name = "走进摄影师得世界",
                CourseCode = "009",
                Maxpeople = 40,
                ChoosePeople = 0,
                TeacherList = Container.Instance.Resolve<ITeacherService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 2 }) }),
                Remark = "把世间之美用视频的方式保存下来"
            });
            //id=10
            Container.Instance.Resolve<ICourseService>().Add(new Course()
            {
                Name = "一个演员的自我修养",
                CourseCode = "010",
                Maxpeople = 40,
                ChoosePeople = 0,
                TeacherList = Container.Instance.Resolve<ITeacherService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 2 }) }),
                Remark = "了解到演员的生活"
            });
            //id=11
            Container.Instance.Resolve<ICourseService>().Add(new Course()
            {
                Name = "拖延心理学",
                CourseCode = "011",
                Maxpeople = 60,
                ChoosePeople = 24,
                TeacherList = Container.Instance.Resolve<ITeacherService>().Query(new List<ICriterion>() { Expression.In("ID", new int[] { 5 }) }),
                Remark = "告别等一下，下一次，明天再做。就现在，马上行动！"
            });
        }

        //private void InitStudent_Course()
        //{
        //    Container.Instance.Resolve<IStudent_CourseService>().Add(new Student_Course()
        //    {

        //    });
        //}
    }
}